<?php
/**
 * Created by JetBrains PhpStorm.
 * User: Binh
 * Date: 1/7/14
 * Time: 12:27 AM
 * To change this template use File | Settings | File Templates.
 */
use \Model;

class Controller_Update extends Controller{
    private $view;
    private $error;

    public function __construct(){
        $this->error = "";

        //Cài đặt view
        $this->view = View::forge('home/layout');
        $this->view->head = View::forge('home/head');
        $this->view->header = View::forge('home/header');
        $this->view->footer = View::forge('home/footer');
        $this->view->layout_update_song = View::forge('home/layout_update_song');

        //Cài đặt biến toàn cục
        $this->view->set_global('site', 'layout_update_song');
        $this->set_error($this->error);
    }

    private function set_error($string){
        $this->view->set_global('error', $string);
    }
    private function set_layout($genres){
        $this->view->set_global('genres', $genres);

        return $this->view;
    }

    public function action_song(){
        return $this->set_layout(\Model\Genre::get_all());
    }

    public function action_update_song(){
        if(!isset($_POST['path_file'])){
            $this->set_error("Chưa nhập file nhạc");
            return $this->action_song();
        }
        $path_file = $_POST['path_file'];
        if(!$_POST['song_name']){
            $song_name = "Chưa biết";
        }
        else $song_name = $_POST['song_name'];
        if(!$_POST['singer_name']){
            $singer_name = "Chưa biết";
        }
        else $singer_name = $_POST['singer_name'];
        if(!$_POST['author_name']){
            $author_name = "Chưa biết";
        }
        else $author_name = $_POST['author_name'];

        date_default_timezone_set('Asia/Ho_Chi_Minh');
        $now = getdate();
        $date = $now['year'].'-'.$now['mon'].'-'.$now['yday'].' '.$now['hours'].':'.$now['minutes'].':'.$now['seconds'];

        $song = array(
            'SONG_ID' => $this->get_id_new(),
            'SONG_NAME' => $song_name,
            'SINGER_ID' => \Model\Singer::get_id_singer($singer_name)[0]['SINGER_ID'],
            'AUTHOR_ID' => \Model\Author::get_id_author($author_name)[0]['AUTHOR_ID'],
            'GENRE_ID' => $_POST['genre'],
            'UPLOADER_ID' => 'UR000001',
            'VIEW' => 0,
            'FAVORITE' => 0,
            'DATE_UPLOAD' => $date,
            'LYRICS' => '',
            'PATH' => $path_file,
        );
        \Model\Song::insert($song);
        return $this->action_song();
    }

    private function get_id_new(){
        $ids = \Model\Song::get_all();
        if(!$ids){
            return "SO1";
        }
        else{
            $number = array();
            foreach($ids as $id){
                $id = $id['SONG_ID'];
                $number[] =  substr($id, 2, strlen($id));
            }

            sort($number);

            $result = $number[count($number) - 1] + 1;

            return "SO".$result;
        }
    }
}